﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using NUnit.Framework;
using UnitTestUtils.UI;

namespace UnitTestUtilsTests.ViewModelTests
{
    [TestFixture]
    public class ViewModelTests
    {

        [Test]
        public void EnableMethodsWithAsyncOperactions()
        {
            var target = new TestViewModel(TaskScheduler.Current);

            var t = target.SomeMethodSpawningWorkAsync();
            
            var executed = t.Wait(TimeSpan.FromSeconds(5));

            Assert.True(executed);
        }

        public static bool SpinWait(Task task)
        {
            int timeout = Environment.TickCount + (int)UITestHelper.GetTimeout().TotalMilliseconds;

            while (!task.IsCompleted && Environment.TickCount < timeout)
            {
                task.Wait(TimeSpan.FromSeconds(1));
                Console.WriteLine("Spinning");
            }
            return task.IsCompleted;
        }
    }
}
